﻿#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    model=new QStringListModel;

    QStringList list={"上海","北京","南京","成都","达州","江苏","广东"};
    model->setStringList(list);

    ui->listView->setModel(model);
    ui->tableView->setModel(model);


}

MainWindow::~MainWindow()
{
    delete ui;
}


void MainWindow::on_pushButton_add_clicked()
{
    model->insertRow(model->rowCount());
    QModelIndex index=model->index(model->rowCount()-1,0);
    model->setData(index,"新的城市");
    ui->listView->setCurrentIndex(index);
}

void MainWindow::on_pushButton_insert_clicked()
{
    QModelIndex index=ui->listView->currentIndex();
    model->insertRow(index.row());
    model->setData(index,"新的城市");
    ui->listView->setCurrentIndex(index);
}

void MainWindow::on_pushButton_del_clicked()
{
    QModelIndex index=ui->listView->currentIndex();
    model->removeRow(index.row());
}

void MainWindow::on_pushButton_clear_clicked()
{
    model->removeRows(0,model->rowCount());
}

void MainWindow::on_pushButton_init_clicked()
{
    QStringList list={"上海","北京","南京","成都","达州","江苏","广东"};
    model->setStringList(list);
}

void MainWindow::on_pushButton_show_clicked()
{
    ui->plainTextEdit->clear();
    QStringList list=model->stringList();
    for(int i=0;i<list.size();i++)
    {
        ui->plainTextEdit->appendPlainText(list[i]);
    }
}

void MainWindow::on_tableView_clicked(const QModelIndex &index)
{
    ui->label->setText(QString::asprintf("当前项:row=%d,column=%d",index.row(),index.column()));
}
